package jp.nyatla.nyansat;

import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import twitter4j.*;
import twitter4j.json.DataObjectFactory;
import jp.nyatla.nsdb.NtgException;
import jp.nyatla.tap.*;
/**
 * TimeLineの基本クラス
 */
public class ListTimelineTwitterSession extends TwitterSession
{
	public static final String CONSUMER_KEY = "41mzL2o015XptHtXkhYu9g";
	public static final String CONSUMER_SECRET = "8dygkuGra1J7j7mEBXTMNBFxTmMrGLAYXcvzFyrSAA";
	protected void setOAuthConsumer(Twitter i_t)
	{
		i_t.setOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
	}
	
	public long now;
	public ListTimelineTwitterSession(final HttpServletRequest i_req,boolean is_commit) throws TwitterException, NtgException
	{
		super(i_req,is_commit);
		this.now=(new Date()).getTime();
	}
	public static class TlData
	{
		public Status ref_status;
		public String user_sname;
		public String user_name;
		public String user_pf_image;
		public long st_create_at;
		public long st_id;
		public String st_text;
		public Boolean ex_has_my_sname;
		public int user_id;
		public Boolean st_is_retweet;
		public TlData(int i_my_user_id,Status i_status)
		{
			User u=i_status.getUser();
			this.ref_status=i_status;
			this.user_sname=u.getScreenName();
			this.user_name=u.getName();
			this.st_create_at=i_status.getCreatedAt().getTime();
			this.user_pf_image=u.getProfileImageURL().toString();
			this.st_text=i_status.getText();
			this.st_id=i_status.getId();
			this.user_id=u.getId();
			this.st_is_retweet=i_status.isRetweet();
		}
		public static TlData[] createTlDatas(int i_my_user_id,ResponseList<Status> i_status)
		{
			int size=i_status.size();
			TlData[] t=new TlData[size];
			for(int i=0;i<size;i++){
				t[i]=new TlData(i_my_user_id,i_status.get(i));
			}
			return t;
		}
	}
	/**
	 * ユーザのリスト一覧を取得する。
	 * @param i_user_name
	 * @return
	 * @throws Exception
	 */
	public String getListNameJs(String i_user_name) throws Exception
	{
		Twitter t=this.getConnection();
		ResponseList<UserList> l=t.getAllUserLists(i_user_name);
		int s=l.size();
		StringBuilder sb=new StringBuilder();
		sb.append("[");
		for(int i=0;i<s;i++){
			UserList item=l.get(i);
			if(i>0){
				sb.append(",");
			}
			sb.append("{name:\""+item.getName()+"\",id:"+item.getId()+"}");
		}
		sb.append("]");
		return sb.toString();
	}
	public String getRawListTLJson(String i_user_name,int i_list_id)throws Exception
	{
		Twitter t=this.getConnection();
		Paging paging=new Paging();
		ResponseList<Status> l=t.getUserListStatuses(i_user_name,i_list_id,paging);
		return DataObjectFactory.getRawJSON(l);
	}
	
	public String getListTLJson(String i_user_name,int i_list_id)throws Exception
	{
		Twitter t=this.getConnection();
		ResponseList<Status> l=t.getUserListStatuses(i_user_name,i_list_id,null);
		TlData[] tls=TlData.createTlDatas(this._session_data.user_id,l);
		int s=tls.length;
		StringBuilder sb=new StringBuilder();
		sb.append("[");
		for(int i=0;i<s;i++){
			TlData tl=tls[i];
			if(i>0){
				sb.append(",");
			}
			sb.append("{u_sname:\"");sb.append(tl.user_sname);sb.append("\",");
			sb.append("u_name:\"");sb.append(tl.user_name);sb.append("\",");
			sb.append("u_time:");sb.append(tl.st_create_at);sb.append(",");
			sb.append("u_pf_url:\"");sb.append(tl.user_pf_image);sb.append("\",");
			sb.append("html_text:");sb.append(tl.st_text);sb.append(",");//テキストはリンク解析
			sb.append("is_rt:");sb.append(tl.st_is_retweet?"1":"0");sb.append(",");
			if(tl.st_is_retweet){
				sb.append("r_sname:");sb.append("");sb.append(",");
				sb.append("r_count:");sb.append("");sb.append(",");
			}
			sb.append("text:\"");sb.append(tl.st_text);sb.append("\"}");
		}
		sb.append("]");
		return sb.toString();
	}
}
